import axios from 'axios'
import { Message } from 'element-plus'
import store from "../store"
// eslint-disable-next-line no-undef


// 创建axios实例
const service = axios.create({
  timeout: 60000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  // 处理请求头
  const header = store.state.app.header
  if (header && Object.keys(header).length) {
    for (const key in header) {
      // 请求头需要带上系统类型
      config.headers[key] = header[key]
    }
  }
  return config
}, error => {
  // Do something with request error
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    if (response.headers['access-ecs-token'])
      store.commit('SET_TOKEN', response.headers['access-ecs-token'])
    console.log("response", response);
    return {
      code: 1,
      result: response.data
    }
  },
  error => {
    Message.error(error?.response?.data?.msg)
    return Promise.reject(error)
  }
)

export default service
